#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include "ui.h"
void openmusic(int mod,char *str)//0为启动程序时开音乐，1为暂停后继续音乐
{
    char music_opcom[50];
    sprintf(music_opcom,"madplay ./music/%s &",str);
    if(mod == 0)
    {
        system(music_opcom);
        printf("music启动成功\n");
    }
    if(mod == 1)
    {
        system("killall -CONT madplay &");
        printf("music继续成功\n");
    }
}
void stopmusic()
{
    system("killall -STOP madplay &");
    printf("music暂停成功\n");
}
void exitmusic()
{
    system("killall -9 madplay");
    printf("music退出成功\n");
}
struct node
{
    char * data;
    int photo;
    struct node * next;
    struct node * prev;
};

// char * change_char(int changesong)//0无切歌，1上一首，2下一首
// {
//     struct node * head = (struct node*)malloc(sizeof(struct node));
//     head->data = NULL;
//     head->next = head->prev = head;
//     head->photo = 0;
//     struct node * p = head;

//     DIR *dir = opendir("./music");
//     struct dirent * music_p;
//     while((music_p = readdir(dir))!=NULL)
//     {

//         if(strstr(music_p->d_name,".mp3"))
//         {
//             printf("%s\n",music_p->d_name);
//             if(head->data == NULL)
//             {
//                head->data =  music_p->d_name;
//             }
//             else
//             {
//                 struct node * new = (struct node*)malloc(sizeof(struct node));
//                 new->data = music_p->d_name;
//                 p->next = new ;
//                 new->prev = p;
//                 new->next = head;
//                 p = new ;
//             }
//         }
//         head->prev = p ;
//     }
//     struct node * song= (struct node*)malloc(sizeof(struct node));
//     song = head;
//     char * music_com = (char*)malloc(50);
//     switch (changesong)
//     {
//     case 1:
//         song = song->prev;
//         sprintf(music_com,"madplay ./music/%s &",song->data);
//         break;
//     case 2:
//         song = song->next;
//         sprintf(music_com,"madplay ./music/%s &",song->data);
//         break;
//     default:
//         break;
//     }
//     printf("%s",music_com);
//     closedir(dir);
//     return music_com;
// }
// void change_com(char * musiccom)//0无切歌，1上一首，2下一首
// {
//     //char * buf ;
//     //buf = change_char(changesong);
//     exitmusic();
//     system(musiccom);
// }
int main()
{
    int a;//test
    struct node * head = (struct node*)malloc(sizeof(struct node));
    head->data = NULL;
    head->next = head->prev = head;
    struct node * p = head;
    struct node * aaa = head;//test
    DIR *dir = opendir("./music");
    struct dirent * music_p;
    int photonum = 0;
    while((music_p = readdir(dir))!=NULL)
    {

        if(strstr(music_p->d_name,".mp3"))
        {
            printf("photonum=%d,musicname:%s\n",photonum,music_p->d_name);
            if(head->data == NULL)
            {
               head->data =  music_p->d_name;
               head->photo = photonum;
            }
            else
            {
                struct node * new = (struct node*)malloc(sizeof(struct node));
                new->data = music_p->d_name;
                new->photo = photonum;
                p->next = new ;
                new->prev = p;
                new->next = head;
                p = new ;
            }
            photonum++;
        }
    }
    head->prev = p ;
    for(a=0;a<6;a++)
    {
        printf("aaanum=%d,aaaname:%s\n",aaa->photo,aaa->data);
        aaa = aaa->next ;
    }
    struct node * song= head;
    char * music_com = (char*)malloc(50);
    
    printf("%s",music_com);
    closedir(dir);
    ////ui////
    int musicapp=0,music_mod=0,music_photo = 0,music_list = 0;//app：程序启动与否标志位 mod：播放暂停 photo：封面选择
    int cache[800*480];//显示缓存
    int *tmp;
    int lcd = open("/dev/fb0",O_RDWR);   

    int touch_fd = open("/dev/input/event0",O_RDONLY);
    int touch_x,touch_y;
    struct input_event touch;
    int *argb = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);
    int bmp_n=0;
    
    ////ui////
    while(1)
    {
        music_photo = song->photo;
        //ui//
        tmp = ui_music(cache,music_mod,music_photo,music_list);
        for(bmp_n=0;bmp_n<800*480;bmp_n++)
        {
            *(argb+bmp_n) = tmp[bmp_n];
        }//写入显存
        //
        read(touch_fd,&touch,sizeof(touch));
        if(touch.type == EV_ABS && touch.code == ABS_X )
            touch_x=touch.value*800/1024;
        if(touch.type == EV_ABS && touch.code == ABS_Y )
            touch_y=touch.value*480/600;
        //MUSIC
        if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
        {
            printf("x=%d,y=%d\n",touch_x,touch_y);
            if(touch_x>=700 && touch_x <800 &&touch_y>=380)
            {
                printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                //page = 1;//退出跳转首页
                exitmusic();//退出音乐程序
                break;//退出循环
            }
            if(touch_x>0 && touch_x <100 && touch_y>=380 && touch_y<480)
            {
                printf("x=%d,y=%d,歌单\n",touch_x,touch_y);
                music_list++;music_list%=2;
            }//显示歌单
            if(music_list == 1)//歌单开启
            {
                if(touch_x>450 && touch_x <(248+450) && touch_y>=150 && touch_y<380)
                {
                    if(touch_y > 230 && touch_y < 280)
                    {
                        exitmusic();
                        system("madplay ./music/晴天-周杰伦.mp3 &");
                        while(strcmp(song->data,"晴天-周杰伦.mp3")!=0)
                        {
                            song = song->next;
                        }
                    }
                    if(touch_y>280 && touch_y < 330)
                    {
                        exitmusic();
                        system("madplay ./music/faded.mp3 &");
                        while(strcmp(song->data,"faded.mp3")!=0)
                        {
                            song = song->next;
                        }
                    }
                    if(touch_y>330 &&touch_y < 380)
                    {
                        exitmusic();
                        system("madplay ./music/搁浅-周杰伦.mp3 &");
                        while(strcmp(song->data,"搁浅-周杰伦.mp3")!=0)
                        {
                            song = song->next;
                        }
                    }
                }
            }
            if(touch_x>=200 && touch_x<=300 && touch_y>=380 && touch_y<480)//上一首
            {
                printf("x=%d,y=%d,上一首\n",touch_x,touch_y);
                song = song->prev;
                sprintf(music_com,"madplay ./music/%s &",song->data);
                exitmusic();
                system(music_com);
            }//上一首
            if(touch_x>=350 && touch_x<=450 && touch_y>=380 && touch_y<480)
            {
                printf("x=%d,y=%d,暂停播放\n",touch_x,touch_y);
                if(musicapp == 1 )//已启动
                {
                    if(music_mod == 1)//播放中
                    {
                        stopmusic(1);//暂停
                        music_mod = 0;//然后显示播放按钮
                    }
                    else //暂停中
                    {
                        openmusic(1,song->data);
                        music_mod = 1;
                    }
                }
                if(musicapp == 0)//待启动
                {
                    openmusic(0,song->data);
                    musicapp = 1 ;
                    music_mod = 1;//显示暂停按钮
                }               
            }//暂停或播放
            if(touch_x>=510 && touch_x<=600 && touch_y>=380 && touch_y<480)
            {
                printf("x=%d,y=%d,下一首\n",touch_x,touch_y);
                song = song->next;
                sprintf(music_com,"madplay ./music/%s &",song->data);
                exitmusic();
                system(music_com);
            }//下一首
        }            
        //读取触摸坐标        
    }
    printf("结束\n");
    munmap(argb,800*480*4);
    close(lcd);
    close(touch_fd);
    return 0;
}
